package com.blog.service.impl;

import java.io.File;


import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.blog.service.interfaces.IConfigService;
@Service("configService")
public class ConfigService implements IConfigService {
	 int[] articleNums= new int[]{0,0};
	 List<String> fileList = new ArrayList<String>();
	 List<String> folderList = new ArrayList<String>();

	public int[] getMDNumFromDisk(String fpath){  //fpath="/articles"
		String aaa= this.getClass().getResource("").getPath();
		String fileName ="";
		System.out.println(aaa.split("ROOT")[0]+"ROOT");
		aaa=aaa.split("ROOT")[0]+"ROOT";	
		aaa=aaa+fpath;
		System.out.println(aaa);
		String path =aaa;
		int fileCount = 0;
		int folderCount = 0; 
		File d = new File(path);
		File list[] = d.listFiles();
		
		fileList.clear();
		folderList.clear();
		for(int i = 0; i < list.length; i++){
		    if(list[i].isFile()){
		        fileCount++;
		        String fName = list[i].toString();
		        
		        if(fName.lastIndexOf("\\")>0){
		        	//win
		        	fileName = fName.substring(fName.lastIndexOf("\\")+1).replace(".md","");
		        }else{
		        	fileName = fName.substring(fName.lastIndexOf("/")+1).replace(".md","");
		        }
		        fileList.add(fileName);
		    }else{
		        folderCount++;
		        String fName = list[i].toString();
		        if(fName.lastIndexOf("\\")>0){
		        	fileName = fName.substring(fName.lastIndexOf("\\")+1).replace(".md","");
		        }else{
		        	fileName = fName.substring(fName.lastIndexOf("/")+1).replace(".md","");
		        }
		        folderList.add(fileName);
		    }
		}
		 
		//System.out.println("文件个数："+fileCount);
		//System.out.println("文件夹数："+folderCount);
		//System.out.println(articleNums);
		articleNums[0]=fileCount;
		articleNums[1]=folderCount;
		return articleNums;
	}
	
	public List<String> getMDListFromDisk(){
		
		return fileList;
	}
}
